﻿/// <reference path="../lib/knockout-2.2.0.js" />
/// <reference path="datacontext.js" />
/// <reference path="config.js" />

/**
 @title:        dataprimer
 @description:  Call datacontext to get data from the server. Preload initial data
 @author:       Yuriy Shapovalov (shapovalov.yuri@gmail.com)
 @todo:         1. Redesign system to not download all data in one request, but perform first request to required section,
                   and request remaining data after that.
 */
define('dataprimer',
    ['ko', 'datacontext', 'config'],
    function (ko, datacontext, config) {
        var logger = config.logger,

            /**
              @description: request data from the server
             */
            fetch = function () {
                return $.Deferred(function (def) {
                    var data = {
                        workspace: ko.observable(),
                        statistics: ko.observable(),
                        settings: ko.observable()
                    };

                    $.when(
                        datacontext.workspace.getData({ results: data.workspace }) // request data from datacontext and map it to data.workspace (ko.observable) field
                        //datacontext.statistics.getData({ results: data.statistics }),
                        //datacontext.settings.getData({results: data.settings })
                    )
                    .pipe(function () {
                        logger.success("Fetched data completed");
                    })
                    .fail(function () { def.reject(); })
                    .done(function () { def.resolve(); })
                }).promise();
            };
        return {
            fetch: fetch
        };
    }
);